package elka.sag.logisticsystem.app;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

import elka.sag.logisticsystem.actor.ControlActor;
import elka.sag.logisticsystem.message.ProcessMessage;
import elka.sag.logisticsystem.service.RepositoryService;
import elka.sag.logisticsystem.service.RuleService;
import elka.sag.logisticsystem.service.impl.DroolsRuleService;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.actor.UntypedActorFactory;

public class LogisticSystemApplication {
	private static Logger LOGGER = Logger.getLogger(LogisticSystemApplication.class);
	
	private static RepositoryService repositoryService;
	private static RuleService ruleService;

	static {
		try {
			PropertyConfigurator.configure (new ClassPathResource("log4j.properties").getInputStream());
		} catch (IOException e) {
			LOGGER.error("Blad czytania pliku konfiguracji logowania");
		}
	}
	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "application-config.xml", "infrastructure-config.xml" });
		repositoryService = context.getBean("jdbcRepositoryService",
				RepositoryService.class);
		ruleService = context.getBean("ruleService", DroolsRuleService.class);

		// Create an Akka system
		ActorSystem system = ActorSystem.create("LogisticSystem");

		// create the master
		@SuppressWarnings("serial")
		ActorRef master = system.actorOf(new Props(new UntypedActorFactory() {

			public UntypedActor create() {
				return new ControlActor(repositoryService, ruleService);
			}
		}), "ControlActor");

		// start the calculation
		master.tell(new ProcessMessage());
	}

}
